'use strict';

angular.module('mainApp')
  .controller('TreasureController', function ($scope, treasureService, treasureControllerUIListener) {
      var _treasureControllerUIListener = null;
	  init();
      function init() {
          $scope.searchTemplateName = '';
	   	  $scope.newTreasureTemplateName = '';
	   	  $scope.selectedTreasureTemplate = null;
          $scope.treasureTemplates = [];
          _treasureControllerUIListener = new treasureControllerUIListener($scope, treasureService);
      };
     
     
     $scope.handleFilePreview = function(file){
    	 var videoElement = $("#videoElement");
    	 var video = document.querySelector('video');
    	 var audioElement = $("#audioElement");
    	 var audio = document.querySelector('audio');
    	 var imageElement = $("#imageElement");
    	 // hide any previous element
    	 video.pause();
    	 audio.pause();
    	 videoElement.hide();
    	 audioElement.hide();
    	 imageElement.hide();
    	 
    	 // change the regex to something proper!!
		 var videoPattern = new RegExp("video");
		 var audioPattern = new RegExp("audio");
		 var imagePattern = new RegExp("image");
		 if(videoPattern.test(file.type) == true){
			 $scope.handleVideoFilePreview(file, videoElement, video);
		 }
		 else if(audioPattern.test(file.type) == true){
			 $scope.handleAudioFilePreview(file, audioElement);
		 }
		 else if(imagePattern.test(file.type) == true){
	    	 $scope.handlePictureFilePreview(file, imageElement);
		 }
		 else{
			 alert("Video file format not playable");	 
		 }
     };
     
     $scope.handleVideoFilePreview = function(file, videoElement, video){
		 var objectURL = URL.createObjectURL(file);
		 videoElement.show();
		 video.src = objectURL;
     };
     
     $scope.handleAudioFilePreview = function(file, audioElement){
		 var objectURL = URL.createObjectURL(file);
		 audioElement.show();
		 audioElement.attr("src", objectURL);
     };
     
     $scope.handlePictureFilePreview = function(file, imageElement){
		 var objectURL = URL.createObjectURL(file);
    	 imageElement.attr("src", objectURL);
    	 imageElement.show();
     };
	 
	 
	 $scope.updateTreasureTemplate = function(){	
	     var onSuccess = function(treasureTemplate){
	       copyAttributes(treasureTemplate, $scope.selectedTreasureTemplate);
	       $scope.showMessageDialog("Request Success", "Treasure template successfully saved.", false); 
	     };
	     var onFailure = function(message){
	    	$scope.showMessageDialog("Request Failure", message, true);
	   	 };	 
	     var onError = function(status, message){
	    	$scope.showMessageDialog("Request Error", message, true);
	     };
		 treasureService.updateTreasureTemplate($scope.selectedTreasureTemplate, onSuccess, onFailure, onError);
	 };
	  
     $scope.clearNewTreasureTemplateDialog = function (){
	   	   var modalErrorLabel = $("#newTreasureTemplateErrorLbl");
	   	   modalErrorLabel.text("");
	   	   $scope.newTreasureTemplateName = '';
		   modalErrorLabel.hide();
     };     
    
	 
	 // move this out to a helper later
	 $scope.showMessageDialog = function(title, message, isError){
		 var messageDialogMessageLbl = $("#messageDialogMessageLbl");
		 var messageDialogErrorLbl = $("#messageDialogErrorLbl");
		 messageDialogMessageLbl.hide();
		 messageDialogErrorLbl.hide();

		 $("#messageDialogTitle").html(title);
		 if(isError == false){
			 messageDialogMessageLbl.html(message);
			 messageDialogMessageLbl.show();
		 }
		 else{
			 messageDialogErrorLbl.html(message);
			 messageDialogErrorLbl.show(); 
		 }
	     $("#messageDialog").modal('show');
	 };
	 
	 
	 // Move this out to a utility 
	 var copyAttributes = function(objectFrom, objectTo){
		 for(var attribute in objectFrom) {
			 objectTo[attribute]=objectFrom[attribute];
		 };
	 };
	 
	  
  });


